﻿// ***********************************************************************
// Assembly         : DonNicky.Common
// Author           : donnicky
// Created          : 11-19-2012
//
// Last Modified By : donnicky
// Last Modified On : 11-16-2012
// ***********************************************************************
// <copyright file="AssemblyExtensions.cs" company="DonNicky">
//     Copyright (c) DonNicky 2012. All rights reserved.
// </copyright>
// <summary>AssemblyExtensions class</summary>
// ***********************************************************************
using System;
using System.Reflection;

namespace DonNicky.Common
{
    /// <summary>
    /// Assembly extensions class.
    /// </summary>
    public static class AssemblyExtensions
    {
        /// <summary>
        /// Gets the product name of an assembly.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns>System.String.</returns>
        public static string GetProduct(this Assembly assembly)
        {
#if NET45
            return assembly.GetCustomAttribute<AssemblyProductAttribute>().Product;
#elif NET4 || SL5 || PORTABLE
            return ((AssemblyProductAttribute)
                 Attribute.GetCustomAttribute(assembly, typeof(AssemblyProductAttribute))).Product;
#endif
        }

        /// <summary>
        /// Gets the version.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns>Version.</returns>
        public static Version GetVersion(this Assembly assembly)
        {
#if NET4 || NET45
            return assembly.GetName().Version;
#elif SL5 || PORTABLE
            var ver = assembly.FullName.Substring(assembly.FullName.IndexOf("Version=", StringComparison.Ordinal) + 8);
            ver = ver.Substring(0, ver.IndexOf(','));
            return new Version(ver);
#endif
        }

        /// <summary>
        /// Gets the version in text format.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns>System.String.</returns>
        public static string GetVersionText(this Assembly assembly)
        {
            var v = assembly.GetVersion();
            return string.Format("{0}.{1}{2}{3}", v.Major, v.Minor, (v.Build > 0 || v.Revision > 0 ? "." + v.Build : ""),
                                 (v.Revision > 0 ? "." + v.Revision : ""));
        }

        /// <summary>
        /// Gets the copyright info.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns>System.String.</returns>
        public static string GetCopyright(this Assembly assembly)
        {
#if NET45
            return assembly.GetCustomAttribute<AssemblyCopyrightAttribute>().Copyright;
#elif NET4 || SL5 || PORTABLE
            return ((AssemblyCopyrightAttribute)
                 Attribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute))).Copyright;
#endif
        }
    }
}